package com.zhuoyue.web.controller.v2;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.domain.AppUserDO;
import com.zhuoyue.biz.core.api.domain.HomeDO;
import com.zhuoyue.biz.core.api.dto.rep.HomeRepDTO;
import com.zhuoyue.biz.core.api.dto.req.HomeReqDTO;
import com.zhuoyue.biz.core.api.service.impl.HomeService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.aop.code.SmsCode;
import com.zhuoyue.common.aop.version.ApiVersion;
import com.zhuoyue.common.constant.ExtrasCode;
import com.zhuoyue.common.constant.MessageCode;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.group.ValidationGroups;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wjming
 * @ClassName: AppHomeController
 * @Description:用户家庭信息添加，获取，删除，修改操作
 * @Date 2021/2/14 19:55
 */
@RestController
@Scope("prototype")
@Validated
public class AppHomeV2Controller {


    @Autowired
    private HomeService homeService;


    /**
     * 图片默认是:home_01
     *
     * @param request
     * @param homeReqDTO
     * @return
     */
//    @PostMapping("/api/home/add")
//    @CheckLogin
//    @ApiVersion(value = "1.4.0")
    public ControllerResult addHome(HttpServletRequest request, @Validated({ValidationGroups.Insert.class}) @RequestBody HomeReqDTO homeReqDTO) {

        User user = (User) request.getAttribute("user");
        HomeDO home = new HomeDO().homeReqDtoToHomeDo(homeReqDTO);

        HomeRepDTO homeRepDTO = homeService.addHomeAndRepDTO(home, user.getId());
        return ControllerResult.toReturnDataSuccess("家庭添加成功", homeRepDTO);
    }


    /**
     * 修改家庭信息
     *
     * @return
     */
//    @PutMapping("/api/home/modify")
//    @CheckLogin
//    @ApiVersion(value = "1.4.0")
    public ControllerResult updateHomeInfo(@Validated({ValidationGroups.Update.class}) @RequestBody HomeReqDTO homeReqDTO) {
        HomeDO home = new HomeDO().homeReqDtoToHomeDo(homeReqDTO);
        boolean result = homeService.updateById(home);
        if (!result) {
            return ControllerResult.toParamFail("修改失败");
        }
        return ControllerResult.toReturnMessageSuccess("修改成功");
    }
}
